﻿namespace TuneMultiCheckBox.Plugins.Common.TuneMultiCheckBox
{
  using BusinessLogic;
  using Logic.TuneMultiCheckBox;
  using Microsoft.Xrm.Sdk;

  public class UpdateConfig : PluginBase<Entity>, IPlugin
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="UpdateConfig" /> class.
    /// </summary>
    public UpdateConfig()
      : base(typeof(SaveCheckboxSet))
    {
      this.RegisteredPluginSteps.Add(new PluginStepBase
      {
        Stage = Stage.PostOperation,
        Mode = Mode.Synchronous,
        MessageName = MessageName.Publish,
        Handler = this.ProcessPublish
      });

      this.RegisteredPluginSteps.Add(new PluginStepBase
      {
        Stage = Stage.PostOperation,
        Mode = Mode.Synchronous,
        MessageName = MessageName.PublishAll,
        Handler = this.ProcessPublishAll
      });
    }

    /// <summary>
    /// Processes the publish.
    /// </summary>
    /// <param name="localContext">The local context.</param>
    private void ProcessPublish(LocalPluginContext localContext)
    {
      Manager.UpdateConfigAsNeeded(localContext);
    }

    /// <summary>
    /// Processes the publish all.
    /// </summary>
    /// <param name="localContext">The local context.</param>
    private void ProcessPublishAll(LocalPluginContext localContext)
    {
      Manager.UpdateConfig(localContext);
    }
  }
}
